package com.bw.common.utils

import java.text.SimpleDateFormat

class MyTimeUtils {


    companion object {
        private val allTimeSdf: SimpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        private val allTimeSdf2: SimpleDateFormat = SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
        private val hourSdf: SimpleDateFormat = SimpleDateFormat("HH:mm")


        /**
         * 把yyyy-MM-dd HH:mm:ss字符串转换成 HH:mm格式
         */
        fun allTimeToHour(allStr: String): String {
            return try {
                hourSdf.format(allTimeSdf.parse(allStr))
            } catch (e: Error) {
                "时间转换错误"
            }
        }

        /**
         * 把yyyy/MM/dd HH:mm:ss字符串转换成 距离现在的天数字符串
         */
        fun allTimeToDay(input: String): String {
            return try {
                val parse = allTimeSdf2.parse(input)
                val today = (System.currentTimeMillis() - parse.time) / 1000 / 60 / 60 / 24
                when (today.toInt()) {
                    0 -> "今天"
                    1 -> "昨天"
                    2 -> "前天"
                    else -> "${today}天前"
                }

            } catch (e: Error) {
                "时间转换错误"
            }
        }

        /**
         * 获取当前时间 yyyy/MM/dd HH:mm:ss字符串
         */
        fun getNowTimeStr():String{
            return try {
                allTimeSdf.format(System.currentTimeMillis())
            } catch (e:Error){
                "时间转换错误"
            }
        }



    }

}